package com.crm.controller;

import com.crm.DTO.ActivityDto;
import com.crm.common.result.ResultDate;
import com.crm.common.result.ResultVO;
import com.crm.common.result.TableDataInfo;
import com.crm.entity.Activity;
import com.crm.service.ActivityService;
import com.crm.vo.ActivityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动管理
 */
@RestController
@Slf4j
@RequestMapping("/clues/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 根据渠道查询对应的活动列表
     * @param channel
     * @return
     */
    @GetMapping("/listselect/{channel}")
    public ResultVO<List<Activity>> listActivityByChannel(@PathVariable Integer channel){
        List<Activity> list = activityService.listActivityByChannel(channel);
        return ResultVO.success(list);
    }

    /**
     * 分页查询活动列表
     * @param activityDto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<Activity> listPage(ActivityDto activityDto){



        ActivityVo list = activityService.listPage(activityDto);
        TableDataInfo<Activity> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(list.getActivities());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setTotal(list.getTotalCount());
        return tableDataInfo;

    }

    /**
     * 新增活动(待测试)
     */
    @PostMapping
    public TableDataInfo saveActivity(@RequestBody ActivityDto activityDto){
        activityService.saveActivity(activityDto);
        TableDataInfo<Activity> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("操作成功");
        return tableDataInfo;
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{id}")
    public TableDataInfo deleteActivity(@PathVariable Long id){
        activityService.deleteActivity(id);
        TableDataInfo<Activity> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("操作成功");
        return tableDataInfo;
    }

    /**
     * 回显活动数据
     */
    @GetMapping("/{id}")
    public ResultDate<Activity> getActivityById(@PathVariable Long id){
        Activity activity = activityService.getActivityById(id);
        return ResultDate.success(activity);
    }

    /**
     * 修改活动
     */
    @PutMapping
    public ResultDate updateActivity(@RequestBody ActivityDto activityDto){
        log.info("修改活动id{}", activityDto.getId());
        activityService.updateActivity(activityDto);
        return ResultDate.success();
    }
 }
